package com.hl.sfzy.controller.course;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.course.Course;
import com.hl.sfzy.service.course.CourseAllService;
import com.hl.sfzy.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.UUID;

/**
 * @program: sfzy
 * @description: 课程主页面
 * @author: xiaoyan
 * @create: 2024-09-05 15:27
 **/

@RestController
@RequestMapping("/course")
public class CourseAllController {
    @Autowired
    private CourseAllService courseAllService;

    @GetMapping("/all")
    public R<PageInfo<Course>> courseAllContent(Integer pageNo, Integer pageSize) {
        PageInfo<Course> coursePageInfo = courseAllService.courseAllContent(pageNo, pageSize);
        return R.success(coursePageInfo);
    }

    @GetMapping("/details")
    public R<Course> courseAllContentById(Long id) {
        Course course = courseAllService.courseAllContentById(id);
        return course != null ? R.success(course) : R.failure("500");
    }

    //查看图片
    @GetMapping("/downImg")
    public void down(String file, HttpServletResponse resp) throws Exception {
        String path = "d://picture//" + file;
        FileInputStream inputStream = new FileInputStream(path);
        ServletOutputStream outputStream = resp.getOutputStream();

        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        while (i != -1) {
            outputStream.write(bytes, 0, i);
            i = inputStream.read(bytes);
        }
        outputStream.close();
        inputStream.close();

    }


//    @PostMapping("/fileup")
//    public void up(@RequestBody MultipartFile file) throws Exception {
//        // 文件原始名称
//        String originalFilename = file.getOriginalFilename();
//        File file1 = new File("D:\\picture\\");
//
//        if (!file1.exists()){
//            file1.mkdir();
//        }
//
//        int i = originalFilename.lastIndexOf(".");
//        String suffix = originalFilename.substring(i);
//        String filename = UUID.randomUUID().toString().substring(1, 7).toString().concat(suffix);
//
//        InputStream inputStream = file.getInputStream();
//
//        FileOutputStream fileOutputStream = new FileOutputStream("D:\\picture\\"+filename);
//        FileCopyUtils.copy(inputStream,fileOutputStream);
//    }

    //下载图片到路径
    @PostMapping("/fileup")
    public R<String> upload(MultipartFile file) throws IOException {
        String path = "D://picture//";
        File file1 = new File(path);
        if (!file1.exists()){
            file1.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
        path = path + filename;
        file.transferTo(new File(path));
        return R.success(filename);
    }

    //新增课程
    @PostMapping("/add")
    public R<Course> courseInsert(Course course){
        int i = courseAllService.courseInsert(course);
        return i>0?R.success(course):R.failure("500");
    }

    //删除课程
    @DeleteMapping("/delete")
    public R<Integer> courseDelete(Long id){
        int i=courseAllService.courseDelete(id);
        return R.success(i);
    }

    //修改课程
    @PutMapping("/update")
    public R<Course> courseUpdate(Course course){
        System.out.println(course);
        int i = courseAllService.updateCourse(course);
        return R.success(course);
    }

    @GetMapping("/like")
    public R<PageInfo<Course>> courseLikeSearch(Integer pageNo, Integer pageSize, String courseName,  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date startTime,  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        PageInfo<Course> coursePageInfo = courseAllService.courseLikeSearch(pageNo, pageSize,startTime,endTime,courseName);
        return R.success(coursePageInfo);
    }

}
